<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :permission="permissionList"
        :page.sync="page"
        :row-style="rowStyle"
      >
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="myAdd">添加</el-button>
          <el-button
            type="primary"
            size="small"
            @click="confirms"
            v-permission="permissionList.confirmBtn"
            >清点人确认</el-button
          >
          <el-button type="primary" size="small" @click="delBO">删除</el-button>
          <el-button type="primary" size="small" v-if="false" @click="handleExport"
            >导出</el-button
          >
          <el-button type="primary" size="mini" @click="submitData">加入报表差错</el-button>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            :size="scope.size"
            :type="scope.type"
            v-if="scope.row.approvalStatus == '0'"
            @click.stop="myEdit(scope.row)"
            >编 辑</el-button
          >
          <el-button
            icon="el-icon-view"
            :size="scope.size"
            :type="scope.type"
            @click.stop="myView(scope.row)"
            >预 览</el-button
          >
          <el-button
            icon="el-icon-check"
            :size="scope.size"
            :type="scope.type"
            @click="openPastRecords(scope.row)"
            >查看历史记录</el-button
          >
        </template>
      </avue-crud>
      <open-dialog
        :DialogVisible.sync="addDialogVisible"
        title="添 加"
        width="80%"
        :before-close="beforeAddClose"
        @close="getList(page)"
      >
      <avue-form v-if="addDialogVisible"
        v-model="formCheckDetail"
        :option="formCheckOption"
      ></avue-form>
        <el-tabs v-model="active" v-if="addDialogVisible" style="margin-top: -40px;">
          <el-tab-pane label="硬币回收箱" name="1">
            <coin
              :thisData="coinData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :revenue="value"
              :addDis="true"
              ref="coin"
              myType="0"
            ></coin>
          </el-tab-pane>
          <el-tab-pane label="硬币补充箱" name="2">
            <coin-change
              :thisData="coinChangeData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :revenue="value"
              :addDis="true"
              ref="coinChange"
              myType="0"
            ></coin-change>
          </el-tab-pane>
          <el-tab-pane label="纸币回收箱" name="3">
            <paper
              :thisData="paperData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :revenue="value"
              :addDis="true"
              ref="paper"
              myType="0"
            ></paper>
          </el-tab-pane>
          <el-tab-pane label="纸币找零箱" name="4">
            <paper-change
              :thisData="paperChangeData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :revenue="value"
              :addDis="true"
              ref="paperChange"
              myType="0"
            ></paper-change>
          </el-tab-pane>
          <el-tab-pane label="废钞箱" name="5">
            <waste
              :thisData="wasteData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :revenue="value"
              :addDis="true"
              ref="waste"
              myType="0"
            ></waste>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="editDialogVisible"
        title="编 辑"
        width="80%"
        :before-close="beforeEditClose"
        @close="getList(page)"
      >
      <avue-form v-if="editDialogVisible"
        v-model="formCheckDetail"
        :option="formCheckOptionEdit"
      ></avue-form>
        <el-tabs v-model="active" v-if="editDialogVisible" style="margin-top: -40px;">
          <el-tab-pane label="硬币回收箱" name="1">
            <coin
              :thisData="coinData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :editDis="true"
              ref="coin"
              myType="1"
            ></coin>
          </el-tab-pane>
          <el-tab-pane label="硬币补充箱" name="2">
            <coin-change
              :thisData="coinChangeData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :editDis="true"
              ref="coinChange"
              myType="1"
            ></coin-change>
          </el-tab-pane>
          <el-tab-pane label="纸币回收箱" name="3">
            <paper
              :thisData="paperData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :editDis="true"
              ref="paper"
              myType="1"
            ></paper>
          </el-tab-pane>
          <el-tab-pane label="纸币找零箱" name="4">
            <paper-change
              :thisData="paperChangeData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :editDis="true"
              ref="paperChange"
              myType="1"
            ></paper-change>
          </el-tab-pane>
          <el-tab-pane label="废钞箱" name="5">
            <waste
              :thisData="wasteData"
              :ThisMain="mainData"
              :details="formCheckDetail"
              :editDis="true"
              ref="waste"
              myType="1"
            ></waste>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>

      <open-dialog
        :DialogVisible.sync="viewDialogVisible"
        title="预 览"
        width="80%"
      >
      <avue-form v-if="viewDialogVisible"
        v-model="formCheckDetail"
        :option="formCheckOptionView"
      ></avue-form>
        <el-tabs v-model="active">
          <el-tab-pane label="硬币回收箱" name="1" v-if="viewDialogVisible" style="margin-top: -40px;">
            <coin
              :thisData="coinData"
              :ThisMain="mainData"
              :viewDis="true"
            ></coin>
          </el-tab-pane>
          <el-tab-pane label="硬币补充箱" name="2">
            <coin-change
              :thisData="coinChangeData"
              :ThisMain="mainData"
              :viewDis="true"
            ></coin-change>
          </el-tab-pane>
          <el-tab-pane label="纸币回收箱" name="3">
            <paper
              :thisData="paperData"
              :ThisMain="mainData"
              :viewDis="true"
            ></paper>
          </el-tab-pane>
          <el-tab-pane label="纸币找零箱" name="4">
            <paper-change
              :thisData="paperChangeData"
              :ThisMain="mainData"
              :viewDis="true"
            ></paper-change>
          </el-tab-pane>
          <el-tab-pane label="废钞箱" name="5">
            <waste
              :thisData="wasteData"
              :ThisMain="mainData"
              :viewDis="true"
            ></waste>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>
      <open-dialog title="历史记录" :DialogVisible.sync="visableDialogRecords">
        <el-form label-width="60px">
          <avue-crud
            :data="tableDataRecords"
            :option="formCheckOptionRecords"
          >
            <template slot="menu" slot-scope="scope">
              <el-button
                icon="el-icon-check"
                :size="scope.size"
                :type="scope.type"
                @click="openRecordsDetail(scope.row,scope.index)"
                >查看明细记录</el-button
              >
            </template>
          </avue-crud>
          <div class="bottom-menu">
            <el-button type="primary" @click="visableDialogRecords = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="visableDialogRecordsDetail"
        title="历史明细记录"
        width="80%"
      >
        <el-tabs v-model="activeRecords">
          <el-tab-pane label="硬币回收箱" name="1" style="margin-top: -40px;">
            <coin
              :thisDataRecords="coinDataRecords"
              :ThisMainRecords="mainDataRecords"
              :recordsDis="true"
            ></coin>
          </el-tab-pane>
          <el-tab-pane label="硬币补充箱" name="2">
            <coin-change
              :thisDataRecords="coinChangeDataRecords"
              :ThisMainRecords="mainDataRecords"
              :recordsDis="true"
            ></coin-change>
          </el-tab-pane>
          <el-tab-pane label="纸币回收箱" name="3">
            <paper
              :thisDataRecords="paperDataRecords"
              :ThisMainRecords="mainDataRecords"
              :recordsDis="true"
            ></paper>
          </el-tab-pane>
          <el-tab-pane label="纸币找零箱" name="4">
            <paper-change
              :thisDataRecords="paperChangeDataRecords"
              :ThisMainRecords="mainDataRecords"
              :recordsDis="true"
            ></paper-change>
          </el-tab-pane>
          <el-tab-pane label="废钞箱" name="5">
            <waste
              :thisDataRecords="wasteDataRecords"
              :ThisMainRecords="mainDataRecords"
              :recordsDis="true"
            ></waste>
          </el-tab-pane>
        </el-tabs>
      </open-dialog>
      <div>
        <el-dialog
          width="30%"
          title="清点人密码"
          :visible="dialogFormVisible"
          append-to-body
          center
          :destroy-on-close="true"
        >
          <el-form :model="form1">
            <el-form-item label="清点人密码" prop="password">
              <el-input
                v-model="form1.password"
                type="password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmUser">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </basic-container>
    <addReportEditor ref="addReportEditor" @addReportSuccess="getList"></addReportEditor>
  </div>
</template>

<script>
import baseCrud from "@/mixins/base-crud";
import { dateFormat } from "@/util/date";
import coin from "./coin";
import coinChange from "./coin-change";
import paperChange from "./paper-change";
import paper from "./paper";
import waste from "./waste";
import { checkPwd } from "@/api/admin/user";
import {
  getObj,
  confirmObj,
  delBatch,
  fetchList,
  queryPastRecords,
  queryPastRecordsDetail,
  getPeriods
} from "@/api/subwaystm/swsttvmcheck/swsttvmcheckdetail";
import {formCheckOption,formCheckOptionEdit,formCheckOptionView,formCheckOptionRecords,formCheckOptionDetailRecords} from "@/const/crud/subwaystm/swsttvmcheck/swsttvmcheckdetail";
import transitUtil from "@/api/subwayRat/transitUtil";
import addReportEditor from '@/views/stationTiecketManager/xianWngCJXXQR/BBCCGG/BBCCGGSQ/Editor';

export default {
  name: "swsttvmcheckdetail",
  mixins: [baseCrud("subwaystm/swsttvmcheck/swsttvmcheckdetail")],
  provide() {
    return {
      closeDialog: this.closeDialog
    };
  },
  components: {
    coin,
    coinChange,
    paperChange,
    paper,
    waste,
    addReportEditor
  },
  data() {
    return {
      formCheckOption: formCheckOption,
      formCheckOptionEdit: formCheckOptionEdit,
      formCheckOptionView: formCheckOptionView,
      formCheckDetail: {},
      value: "",
      dialogFormVisible: false,
      form1: {
        password: ""
      },
      searchForm: {
        approvalStatus: "0"
      },
      addDialogVisible: false,
      viewDialogVisible: false,
      editDialogVisible: false,
      active: "1",
      coinData: [],
      coinChangeData: [],
      paperData: [],
      paperChangeData: [],
      wasteData: [],
      mainData: {},
      mainId: null,
      ids: [],
      checkPersonTwoJopNos: [],
      visableDialogRecords: false,
      visableDialogRecordsDetail: false,
      formCheckOptionRecords: formCheckOptionRecords,
      formCheckOptionDetailRecords: formCheckOptionDetailRecords,
      tableDataRecords: [],
      recordsId: [],
      formCheckDetailRecords: {},
      activeRecords: "1",
      coinDataRecords: [],
      coinChangeDataRecords: [],
      paperDataRecords: [],
      paperChangeDataRecords: [],
      wasteDataRecords: [],
      mainDataRecords: {},
      settleDateAll:{
        startSettleDate:'',
        endSettleDate:''
      },
      selectKeys:[]
    };
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.generator_swsttvmcheckdetail_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.generator_swsttvmcheckdetail_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.generator_swsttvmcheckdetail_edit,
          false
        ),
        confirmBtn: this.vaildData(
          this.permissions.swsttvmcheckdetail_confirm,
          false
        )
      };
    }
  },
  created() {
    console.log('created');
    this.otherVueAction();
  },
  methods: {
     //颜色特殊标识
     rowStyle({ row, column, rowIndex }) {
       if (row.confirmStatus == "0") {
        	return {
			  backgroundColor: "#eee",
			  color: "blue",
			};
	 }
	     
    },
    async submitData() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      if (this.$refs.crud.tableSelect.length != 1) {
        this.$message.error("只能选择一条数据");
        return false;
      }
      let params = {};
      let res = false;
      let ress = false;
      for (const table of this.$refs.crud.tableSelect) {
        if (table.approvalStatus != "1") {
          this.$message.error("请选择已提交的数据");
          return false;
        }else{
          params = {
            lineCode: table.lineCode,
            stationCode: table.stationCode,
            revenueTime: table.revenueTime
          };
          this.getPeriods(params);
          if (table.isAddReprotError == "1") {
            res = await this.$confirm('该数据已加入报表差错，是否重新加入？', '提示', {
              confirmButtonText: '是',
              cancelButtonText: '否',
              type: 'warning'
            });
            if (!res) {
              return;
            }else{
              ress = true;
            }
          }
        }
      }
      if(res==false){
        ress = await this.$confirm("是否加入报表差错", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        });
      }
      if (!ress) {
        return;
      }
      this.selectKeys = this.$refs.crud.tableSelect;
      let param={
        dialogTitle: "加入报表差错",
        diffAmountId:this.selectKeys[0].id,
        dataSourceTable:"sw_st_tvm_check_detail",
        lineCode:this.selectKeys[0].lineCode,
        stationCode:this.selectKeys[0].stationCode,
        tvmCode:this.selectKeys[0].tvmCode,
        reprotTime:this.selectKeys[0].revenueTime,
        reportNameId:5,
        settleBeginTime:this.settleDateAll.startSettleDate,
        settleEndTime:this.settleDateAll.endSettleDate,
        userCodes:this.selectKeys[0].checkPersonTwoJopNo,
	dutyStationLeaderCode: this.selectKeys[0].checkPersonOneJopNo, 
	dutyStationLeaderName: this.selectKeys[0].checkPersonOneName,
        showType:3,
        dataSource:1,
        page:this.page
      };
      this.$refs.addReportEditor.others_report_add_call(param);
    },
    async getPeriods(params){
      await getPeriods(params).then((res)=>{
        if(res!=""){
          let beginTime = res.data.beginTime;
          let endTime = res.data.endTime;
          let now = new Date(beginTime);
          let nows = new Date(endTime);
          this.settleDateAll.startSettleDate = dateFormat(now).substring(0, 10);
          this.settleDateAll.endSettleDate = dateFormat(nows).substring(0, 10);
        }
      });
    },
    async queryPastRecords(row){
      let param={};
      let listRecords = [];
      param.id=row.id;
      var that = this
      await queryPastRecords(param)
        .then(res => {
          listRecords = res.data.data;
          for( var item of listRecords){
            this.tableDataRecords.push(item.logInfo);
            this.recordsId.push(item.id);
          }
        })
    },
    openPastRecords(row){
      this.tableDataRecords = [];
      this.queryPastRecords(row);
      this.visableDialogRecords=true;
    },
    async queryPastRecordsDetail(param){
      await queryPastRecordsDetail(param)
        .then(res => {
          this.coinDataRecords = res.data.data.SW_ST_TVM_CHECK_DETAIL_COIN;
          this.coinChangeDataRecords = res.data.data.SW_ST_TVM_CHECK_DETAIL_COIN_CHANGE;
          this.paperDataRecords = res.data.data.SW_ST_TVM_CHECK_DETAIL_PAPER;
          this.paperChangeDataRecords = res.data.data.SW_ST_TVM_CHECK_DETAIL_PAPER_CHANGE;
          this.wasteDataRecords = res.data.data.SW_ST_TVM_CHECK_DETAIL_WASTE;
        })
    },
    openRecordsDetail(row,index){
      let param={};
      for(var i=0;i<this.recordsId.length;i++){
        if(i==index){
          param.id = this.recordsId[i];
          this.queryPastRecordsDetail(param);
        }
      }
      this.visableDialogRecordsDetail=true;
    },
    beforeEditClose: function(done) {
      this.$confirm("请确认是否已将所有产生数据的钱箱界面点击保存", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$refs.coin.btn = false;
        this.$refs.coinChange.btn = false;
        this.$refs.paperChange.btn = false;
        this.$refs.paper.btn = false;
        this.$refs.waste.btn = false;
        done();
      });
    },
    myAdd: function() {
      for (var i in this.formCheckDetail) {
        this.formCheckDetail[i] = null;
      }
      let now = new Date();
      if (now.getHours() < 6) {
        now.setDate(now.getDate() - 1);
      }
      this.value = dateFormat(now).substring(0, 10);
      this.formCheckDetail.revenueTime = dateFormat(now).substring(0, 10);
      this.formCheckDetail.checkTime = dateFormat(new Date());
      this.coinData = [];
      this.mainId = null;
      this.coinChangeData = [];
      this.paperData = [];
      this.paperChangeData = [];
      this.wasteData = [];
      this.addDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.coin.btn = false;
        this.$refs.coinChange.btn = false;
        this.$refs.paperChange.btn = false;
        this.$refs.paper.btn = false;
        this.$refs.waste.btn = false;
        this.$refs.coin.oneShow = true;
        this.$refs.coin.rowNew = true;
        this.$refs.coinChange.oneShow = true;
        this.$refs.coinChange.rowNew = true;
      });
      this.mainData = {
        checkPersonTwoName: "",
        checkPersonTwoJopNo: "",
        checkTime: "",
        remark: ""
      };
    },
    myView: function(row) {
      getObj(row.id,1).then(response => {
        let res = response.data.data;
        this.value = res.swStTvmCheckDetail.revenueTime;
        this.formCheckDetail = res.swStTvmCheckDetail;
        this.coinData = res.swStTvmCheckDetailCoins;

        this.coinChangeData = res.swStTvmCheckDetailCoinChanges;

        this.paperData = res.swStTvmCheckDetailPapers;

        this.paperChangeData = res.swStTvmCheckDetailPaperChanges;

        this.wasteData = res.swStTvmCheckDetailWastes;
        this.mainData = res.swStTvmCheckDetail;
      });
      this.viewDialogVisible = true;
    },
    myEdit: function(row) {
      getObj(row.id,1).then(response => {
        let res = response.data.data;
        this.value = res.swStTvmCheckDetail.revenueTime;
        this.formCheckDetail = res.swStTvmCheckDetail;
        this.formCheckDetail.revenueTime = res.swStTvmCheckDetail.revenueTime;
        this.coinData = res.swStTvmCheckDetailCoins;
        if (this.coinData != null) {
          for (const item of this.coinData) {
            item.$cellEdit = true;
          }
        }
        this.coinChangeData = res.swStTvmCheckDetailCoinChanges;
        if (this.coinChangeData != null) {
          for (const item of this.coinChangeData) {
            item.$cellEdit = true;
          }
        }
        this.paperData = res.swStTvmCheckDetailPapers;
        if (this.paperData != null) {
          for (const item of this.paperData) {
            item.$cellEdit = true;
          }
        }
        this.paperChangeData = res.swStTvmCheckDetailPaperChanges;
        if (this.paperChangeData != null) {
          for (const item of this.paperChangeData) {
            item.$cellEdit = true;
          }
        }
        this.wasteData = res.swStTvmCheckDetailWastes;
        if (this.wasteData != null) {
          for (const item of this.wasteData) {
            item.$cellEdit = true;
          }
        }
        this.mainData = res.swStTvmCheckDetail;
      });
      // if(row.reportModifyStatus == 1){
      //   getHaveCashboxCode(row.revenueTime,2,2).then(res => {
      //     this.coninChangeList = res.data.data;
      //   });
      //   getHaveCashboxCode(row.revenueTime,4,2).then(res => {
      //     this.paperChangeList = res.data.data;
      //   });
      // }
      this.editDialogVisible = true;
      //   this.formAdd.swStHandworkCleanRecord = {};
      //   this.tableDataAdd = [];
    },
    delBO: function() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.approvalStatus != "0") {
          this.$message.error("请选择未提交的数据");
          return false;
        }
        if (table.reportModifyStatus == "1") {
          this.$message.error("报表修改状态为“是”的情况下，不允许删除数据");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delBatch(ids)
          .then(data => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },

    confirms: function() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      this.checkPersonTwoJopNos = [];
      this.ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.approvalStatus != "0") {
          this.$message.error("请检查数据，只能确认未提交的");
          return false;
        }
        if (table.confirmStatus != "0") {
          this.$message.error("请检查数据，只能确认未确认的");
          return false;
        }
        this.ids.push(table.id);
        this.checkPersonTwoJopNos.push(table.checkPersonTwoJopNo);
        let a = this.checkPersonTwoJopNos[0];
        for (const item of this.checkPersonTwoJopNos) {
          let b = item;
          if (a !== b) {
            this.$message.error("请检查数据，只能选择同一清点人数据进行确认！");
            return false;
          }
        }
      }
      this.dialogFormVisible = true;
    },
    confirmUser: function() {
      if (this.form1.password.length == 0) {
        this.$message.error("请输入清点人密码!");
        return false;
      }
      checkPwd(this.checkPersonTwoJopNos[0], this.form1.password).then(
        response => {
          let data = response.data.data;
          if (data) {
            this.$message.success("输入正确");
            confirmObj(this.ids)
              .then(data => {
                this.$message.success("清点人确认成功！");
                this.getList(this.page);
                done();
              })
              .catch(() => {});
            this.$refs.crud.tableSelect = [];
            this.dialogFormVisible = false;
          } else {
            this.$message.error("用户名或密码不正确！");
          }
        }
      );
    },
    closeDialog() {
      this.addDialogVisible = false;
      this.editDialogVisible = false;
      this.viewDialogVisible = false;
      this.visableDialogRecordsDetail = false;
    },
    beforeAddClose: function(done) {
      this.$confirm("请确认是否已将所有产生数据的钱箱界面点击保存", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$refs.coin.btn = false;
        this.$refs.coinChange.btn = false;
        this.$refs.paperChange.btn = false;
        this.$refs.paper.btn = false;
        this.$refs.waste.btn = false;
        done();
      });
    },
    otherVueAction(){
      transitUtil.$on('updateReport_call_5',(param)=>{
        let p=param;
        fetchList(p).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.bottom-menu {
  margin-top: 48px;
  text-align: center;
}

.upSelect {
}

.rightSelect {
}
</style>
